/*
 * jQuery easyStrValidate Plugin
 * version: 0.01 (2009-04-27)
 *
 * This Program is NEW BSD License
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * It is very easy, and may be useful.
 * This code make it string length validation,
 * and notice message function.
 * 
 * example:
 * 
 * ----------------------------------  * 
 * 
 * <form>
 * <input type="text" id="text" />
 * <p id="valid_message"></p>
 * <input type="submit" />
 * </form>
 * 
 * <script>
 * $(function(){
 *     $('#text').easyStrValidate({
 *                    maxLength : 20,
 *                    messageID : 'valid_message'
 *     });
 * });
 * </script>
 * 
 * ----------------------------------  * 
 * 
 * see also this document (japanese)
 * http://www.alink.co.jp/tech/wiki/index.php?jQuery%2F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89%2F%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%81%AE%E5%85%A5%E5%8A%9B%E6%96%87%E5%AD%97%E6%95%B0%E3%82%92%E5%88%B6%E9%99%90%E3%81%99%E3%82%8B
 * 
 * Copyright (C) 2009 Alink INC. all rights reserved.
 * http://www.alink.co.jp/
 * 
 */

(function(){$.fn.easyStrValidate=function(options){options=$.extend({minLength:5,maxLength:100,messageID:null,allowNullValue:false},options);var $$=$(this);var $message="";if(options.messageID){$message=$("#"+options.messageID);}else{$message=$("<span>");$$.after($message);}
var retrieveMessage=function(n,length){if(length<1&&!options.allowNullValue){return"内容を入力して下さい";}else if(length<options.minLength&&!options.allowNullValue){return options.minLength+"文字以上入力して下さい";}else if(n>0){return"あと"+n+"文字入力できます";}else if(n==0){return"これ以上入力できません";}else{return"入力できる文字数をオーバーしています";}};var toggleSwitch=function(){var length=$$.val().length;var length_diff=options.maxLength-length;if($message.css("display")!="block"){$message.show();}
var $form=$$.parents("form");var $submit=$form.find(":image, :submit");$message.html(retrieveMessage(length_diff,length));if(length>options.maxLength||(length<options.minLength&&!options.allowNullValue)){$submit.attr("disabled","disabled");$form.bind("submit",function(){return false});}else if(length<1&&!options.allowNullValue){$submit.attr("disabled","disabled");$form.bind("submit",function(){return false});}else{$submit.removeAttr("disabled");$form.unbind();}};toggleSwitch();$$.bind("keyup blur click",toggleSwitch);return this;}})(jQuery);